package com.suning.sawp.service.impl.coupon;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.promotion.CouponRecordStatis;
import com.suning.sawp.dto.promotion.InCouponRecord;
import com.suning.sawp.dto.promotion.InnerCouponBlackList;
import com.suning.sawp.intf.dao.coupon.InnerCouponDaoService;
import com.suning.sawp.po.promotion.CouponBlackList;
import com.suning.sawp.po.promotion.InnerCouponRecord;
import com.suning.sawp.service.util.BeanUtil;
import com.suning.sawp.service.util.DateUtils;
/**
 * 
 * 内部优惠券数据操作接口类<br> 
 * 〈功能详细描述〉
 *
 * @author Wuxiao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class InnerCouponDaoServiceImpl implements InnerCouponDaoService{

    @Autowired
    DalClient dalClient;

    @Override
    public int querySendTimesByMonth(String staffId) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 1);
        String startTime = DateUtils.getDateStrByFormat(c.getTime(), DateUtils.DEFAULT_DAY_PATTERN);
        paramMap.put("startTime", startTime);
        paramMap.put("staffId", staffId);
        Map<String, Object> numMap = dalClient.queryForMap("INNER_COUPON_RECORD.SELECT_SEND_TIMES_MONTH", paramMap);
        return numMap != null ? ((Number)numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public int addRecord(InnerCouponRecord couponRecord) {
        return dalClient.persist(couponRecord).intValue();
    }

    @Override
    public List<InCouponRecord> queryRecord(String staffId, Integer pageNo, Integer pageSize) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("staffId", staffId);
        if(pageNo != null && pageSize != null){
            paramMap.put("startIndex", pageSize*(pageNo - 1));
            paramMap.put("maxCount", pageSize);
        }
        return dalClient.queryForList("INNER_COUPON_RECORD.SELECT_RECORD_BY_CON", paramMap,InCouponRecord.class);
    }

    @Override
    public int queryRecordCount(String staffId) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("staffId", staffId);
        Map<String, Object> numMap = dalClient.queryForMap("INNER_COUPON_RECORD.SELECT_RECORD_COUNT_BY_CON", paramMap);
        return numMap != null ? ((Number)numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public List<InnerCouponRecord> queryCouponDetailByActivityId(String staffId, String activityId) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("activityId", activityId);
        return dalClient.queryForList("INNER_COUPON_RECORD.SELECT_RECORD_DETAIL", paramMap,InnerCouponRecord.class);
    }

    @Override
    public List<InnerCouponBlackList> queryBlackList(String staffId, Integer pageSize,
            Integer pageNo) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        if(StringUtils.isNotBlank(staffId)){
            paramMap.put("staffId", staffId);
        }
        if(pageSize != null && pageNo != null){
            paramMap.put("startIndex", pageSize*(pageNo - 1));
            paramMap.put("maxCount", pageSize);
        }
        return dalClient.queryForList("COUPON_BLACK_LIST.SELECT_BLACK_LIST_BY_CON", paramMap,InnerCouponBlackList.class);
    }

    @Override
    public int queryBlackListCount(String staffId) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        if(StringUtils.isNotBlank(staffId)){
            paramMap.put("staffId", staffId);
        }
        Map<String, Object> numMap = dalClient.queryForMap("COUPON_BLACK_LIST.SELECT_BLACK_LIST_COUNT_BY_CON", paramMap);
        return numMap != null ? ((Number)numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public void addBlack(CouponBlackList couponBlackList) {
        Map<String,Object> paramMap = BeanUtil.describe(couponBlackList);
        dalClient.execute("COUPON_BLACK_LIST.INSERT", paramMap);
    }

    @Override
    public void delBlack(String staffId) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("staffId", staffId);
        dalClient.execute("COUPON_BLACK_LIST.DELETE_BLACK_LIST", paramMap);
    }

    @Override
    public List<CouponRecordStatis> queryCouponStatis(String staffId, String startTime,
            String endTime, Integer pageSize, Integer pageNo) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        if(pageSize != null && pageNo != null){
            paramMap.put("startIndex", pageSize*(pageNo - 1));
            paramMap.put("maxCount", pageSize);
        }
        return dalClient.queryForList("INNER_COUPON_RECORD.SELECT_RECORD_STATIS", paramMap,CouponRecordStatis.class);
    }

    @Override
    public int queryCouponStatisCount(String staffId, String startTime, String endTime) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        Map<String, Object> numMap = dalClient.queryForMap("INNER_COUPON_RECORD.SELECT_RECORD_STATIS_COUNT", paramMap);
        return numMap != null ? ((Number)numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public int queryRecordCountByCon(String staffId, String startTime, String endTime) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        Map<String, Object> numMap = dalClient.queryForMap("INNER_COUPON_RECORD.SELECT_RECORD_COUNT_BY_CON", paramMap);
        return numMap != null ? ((Number)numMap.get("NUM")).intValue() : 0;
    }

    @Override
    public List<InnerCouponRecord> queryRecordByCon(String staffId, String startTime,
            String endTime, Integer index, Integer limit) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        if(index != null && limit != null){
            paramMap.put("startIndex", index);
            paramMap.put("maxCount", limit);
        }
        return dalClient.queryForList("INNER_COUPON_RECORD.SELECT_RECORD_DETAIL", paramMap,InnerCouponRecord.class);
    }

    @Override
    public void updateCouponRuleById(String couponRule, Integer Id) {
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("couponRule", couponRule);
        paramMap.put("Id", Id);
        dalClient.execute("INNER_COUPON_RECORD.UPDATE_COUPON_RULE_BY_ID", paramMap);
    }
    
}
